package arxiv;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.Observable;

/*
 * SearchForm.java
 * A form for the user to input keywords, and to display the results
 */


public class SearchForm extends Observable implements ActionListener {

    // member variables

    // frames and panels
    private JFrame frame;
    private JPanel searchPanel;
    private JPanel feedPanel;
    private JPanel contentPane;
    private JEditorPane editorPane;

    // fields
    private JTextField keywordField;
    private JTextField authorField;
    private JTextField titleField;
    private JTextField numResultsField;

    // labels
    private JLabel keywordLabel;
    private JLabel authorLabel;
    private JLabel titleLabel;
    private JLabel numResultsLabel;

    // button
    private JButton searchButton;

    // object to access search data
    private KeywordData mySearchData;

    // constructor
    public SearchForm(KeywordData searchData) {
        mySearchData = searchData;
    	createForm();
        displayFrame();
    }

    // set up form
    private void createForm() {

        // create a frame
        frame = new JFrame("My Reference Finder");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create a panel to input search keywords
        searchPanel = new JPanel();
        // panel to display the search results
        feedPanel = new JPanel();

        // create search fields
        keywordField = new JTextField(20);
        authorField = new JTextField(20);
        titleField = new JTextField(20);
        numResultsField = new JTextField("2",3);

        // create labels for text fields
        keywordLabel = new JLabel("Keyword:");
        keywordLabel.setLabelFor(keywordField);

        authorLabel = new JLabel("Author:");
        authorLabel.setLabelFor(authorField);

        titleLabel = new JLabel("Title:");
        titleLabel.setLabelFor(titleField);

        numResultsLabel = new JLabel("No. Results:");
        numResultsLabel.setLabelFor(numResultsField);

        // add fields and labels to panel
        JLabel[] labels = {keywordLabel, authorLabel, titleLabel, numResultsLabel};
        JTextField[] textFields = {keywordField, authorField, titleField, numResultsField};

        for (int i=0; i < labels.length; i++) {
            searchPanel.add(labels[i]);
            searchPanel.add(textFields[i]);
        }

        // insert search button
        searchButton = new JButton("Search");
        searchPanel.add(searchButton);
        searchButton.addActionListener(this);

        // add a border to the search panel
        searchPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search Fields"),BorderFactory.createEmptyBorder(5,5,5,5)));
        // add a border to the feed panel
        feedPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Query result"),BorderFactory.createEmptyBorder(5,5,5,5)));

        // editor pane for displaying results of query
        editorPane = new JEditorPane();
        editorPane.setEditable(false);
        feedPanel.add(editorPane);


        // add in all panes to frame
        contentPane = new JPanel(new BorderLayout());
        contentPane.add(searchPanel, BorderLayout.PAGE_START);
        contentPane.add(feedPanel, BorderLayout.CENTER);

        frame.getRootPane().setDefaultButton(searchButton);
        frame.setContentPane(contentPane);


    }

    // display frame on screen
    private void displayFrame() {
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
    }

    // on button click, notify controller
    public void actionPerformed(ActionEvent e) {
    	notifyObservers();
    }

    // display results of the query
    public void displayResults(URL queryURL) {
        try {
            editorPane.setPage(queryURL);
        }
        catch (IOException e) {
            System.err.println("Attempted to read a bad URL " + queryURL);
        }
    }

    // no search query to display
    public void displayNoResults() {
        editorPane.setText("No search query entered");
    }

    public boolean fetchUserInput() {
        // get values from text fields
        mySearchData.setKeyText(keywordField.getText());
        mySearchData.setAuthorText(authorField.getText());
        mySearchData.setTitleText(titleField.getText());
        mySearchData.setNumResultsText(numResultsField.getText());

        // test whether any input has been entered
        return mySearchData.hasQuery();
    }


    // access editor pane
    public String getEditorPane() {
        return this.editorPane.getText();
    }

    public void setEditorPane(String editorText) {
        this.editorPane.setText(editorText);
    }
}
